import { JXDef } from "../../../conventions/JXCommon";
import { PRIORITY_VIEW } from "../../../Core/CoreDefine";
import { GCtrl } from "../../../Core/GCtrl";
import GParam from "../../../Core/GEvent/GParam";
import GChild from "../../../Core/GView/GChild";
import ShaderUtil from "../../../Shaders/Manager/ShaderUtil";
import { BOXPREVIEW, CMsg, OFFICERITEM, RedpointEffect, RPointMask } from "../../Common/Define";
import { VIEW_ID } from "../../Common/UI";
import GameMgr from "../../Logic/GameMgr";
import { CURRENCY, ITEM_TYPE } from './../../Common/Define';

const { ccclass, property, menu, executeInEditMode } = cc._decorator;
@ccclass @menu('View/Home/OfficerItem')

export default class OfficerItem extends GChild {


    @property({
        type: cc.Node,
        tooltip: "ad背景"
    })
    adBg: cc.Node = null;

    @property({
        type: cc.Node,
        tooltip: "激励宝箱遮罩"
    })
    adMask: cc.Node = null;

    @property({
        type: cc.Node,
        tooltip: "激励宝箱未解锁"
    })
    adUnlocked: cc.Node = null;

    @property({
        type: cc.Node,
        tooltip: "激励宝箱领取"
    })
    adReceived: cc.Node = null;

    @property({
        type: cc.Node,
        tooltip: "激励宝箱已领取"
    })
    Received: cc.Node = null;


    @property({
        type: cc.Node,
        tooltip: "宝箱背景"
    })
    boxBg: cc.Node = null;


    @property({
        type: cc.Node,
        tooltip: "宝箱遮罩"
    })
    boxMask: cc.Node = null;

    @property({
        type: cc.Node,
        tooltip: "宝箱未解锁"
    })
    boxUnlocked: cc.Node = null;

    @property({
        type: cc.Node,
        tooltip: "宝箱领取"
    })
    boxReceived: cc.Node = null;

    @property({
        type: cc.Node,
        tooltip: "宝箱已领取"
    })
    boReceived: cc.Node = null;



    @property({
        type: cc.Node,
        tooltip: "观看视频宝箱"
    })
    adBox: cc.Node = null;

    @property({
        type: cc.Node,
        tooltip: "宝箱"
    })
    box: cc.Node = null;

    @property({
        type: cc.Label,
        tooltip: "官爵等级"
    })
    lv: cc.Label = null;

    @property({
        type: cc.ProgressBar,
        tooltip: "进度"
    })
    progressBar: cc.ProgressBar = null;

    @property({
        type: cc.Node,
        tooltip: "进度绿色条"
    })
    val: cc.Node = null;

    @property({
        type: cc.Node,
        tooltip: "绿色球"
    })
    green: cc.Node = null;

    @property({
        type: cc.Label,
        tooltip: "官爵功勋数量"
    })
    offNum: cc.Label = null;


    @property({
        type: cc.Node,
        tooltip: "牌子"
    })
    offLv: cc.Node = null;

    /**官爵等级 */
    private _officerLv: number = 0;
    /**爵位升级经验值数组 */
    private _offAllData: [] = [];
    /**每个爵位需要满足的经验 */
    private _upgradeExploitl: number = null;
    /**爵位数据 */
    private _listData: SOfficerDataRaw = null;
    /**爵位索引 */
    private _index: number = null;

    private _curStatus: OfficeBox = null;

    public onGLoad(): void {
        GCtrl.ES.on(CMsg.client.office.onGetAdReward, this, this.onCMSGGetAdReward.bind(this), PRIORITY_VIEW)
        GCtrl.ES.on(CMsg.client.office.onGetReward, this, this.onCMSGGetReward.bind(this), PRIORITY_VIEW);
    }

    protected onCMSGGetAdReward(_, param: GParam) {
        let msg = param.get<OfficeGetReward>();
        if (msg.id === this._listData.id) {
            let str = 'adBox';
            this.initAdBoxImg(str);
        }
    }

    protected onCMSGGetReward(_, param: GParam) {
        let msg = param.get<OfficeGetReward>();
        if (msg.id === this._listData.id) {
            let str = 'box';
            this.initBoxImg(str);
            this.setRedPoint();
            GameMgr.redMgr.sendValueSettingMsg(RPointMask.RPM_Office, false);
        }
    }

    /**初始官爵等级进度条 */
    protected initOfficerLv() {
        let honor = GameMgr.RBagData.getMegreItems(CURRENCY.FEATS, ITEM_TYPE.CURRENCY)
        var val: number = 0;
        if (this._officerLv >= this._listData.id - 1) {
            if (this._officerLv > 0) {
                if (this._index < this._officerLv) {
                    val = 1;
                } else {
                    val = ((honor.num - (this._offAllData[this._officerLv - 1])) / this._upgradeExploitl);
                }
            } else {
                val = honor.num / this._upgradeExploitl;
            }
        }
        // console.log('val2', val);
        if (val >= 1) {
            this.setLoadProgress(1);
        } else if (val <= 0) {
            this.setLoadProgress(0);
        } else {
            let localPos: cc.Vec2 = this.val.convertToWorldSpaceAR(this.green.position);
            // console.log('pos', localPos);
            this.setLoadProgress(val);
        }
    }

    /**初始化 */
    public init(listData: SOfficerDataRaw, index: number, arrs) {
        this._curStatus = GameMgr.lUserData.getOfficeStatus(listData.id);
        this._officerLv = GameMgr.lUserData.officerLv;
        this._offAllData = arrs;
        this._index = index;
        if (this._index > 0) {
            this._upgradeExploitl = Number(Number(this._offAllData[this._index]) - Number(this._offAllData[this._index - 1]))
        } else {
            this._upgradeExploitl = Number(this._offAllData[this._index]);
        }
        this.offNum.string = this._offAllData[this._index] + '';
        this._listData = listData;
        this.initOfficerLv();
        this.setRedPoint();
        this.initAdBoxImg();
        this.initBoxImg();
        this.lv.string = this._listData.name + '';
    }

    public setRedPoint() {
        let isPoint = GameMgr.userMgr.checkOfficeReward(this._listData.id)
        GameMgr.redMgr.setRPointView(this.box, this.box, isPoint, RedpointEffect.REDPOINT)
    }

    protected adClick() {
        if (this._curStatus.adBox == OFFICERITEM.LOCK) {
            GameMgr.sdkMgr.watchAd(() => {
                let msg: OfficeGetReward = {
                    id: this._listData.id
                }
                GCtrl.ES.emit(CMsg.client.office.onGetAdReward, new GParam(msg))
                GameMgr.jumpToMgr.jumpGoTo(VIEW_ID.rewardCtrl, this._listData.award);
                // GameMgr.sdkMgr.umaSetPoint(JXDef.umaPoint.officerCtrl, { msg: PointInfo.officer_ad });
                let v = GameMgr.lUserData.officerLv;
                let str = GameMgr.lUserData.heroOfficer(v);
                GameMgr.sdkMgr.umaSetPoint(JXDef.umaPoint.officerCtrl, { msg: '官职奖励-' + str })//L(JXLocales.tower.officer, this.$signOfficerLv)
            });
        }
    }

    protected click() {
        if (this._curStatus.box == OFFICERITEM.LOCK) {
            let msg: OfficeGetReward = {
                id: this._listData.id
            }
            GCtrl.ES.emit(CMsg.client.office.onGetReward, new GParam(msg))
            GameMgr.jumpToMgr.jumpGoTo(VIEW_ID.rewardCtrl, this._listData.award);
            let v = GameMgr.lUserData.officerLv;
            let str = GameMgr.lUserData.heroOfficer(v);
            GameMgr.sdkMgr.umaSetPoint(JXDef.umaPoint.officerCtrl, { msg: '官职奖励-' + str })//L(JXLocales.tower.officer, this.$signOfficerLv)

            // GameMgr.sdkMgr.umaSetPoint(JXDef.umaPoint.officerCtrl, { msg: PointInfo.officer_norl })
        }

    }
    /**设置激励宝箱图片 */
    protected initAdBoxImg(index?: string) {
        //未解锁
        if (this._curStatus.adBox == OFFICERITEM.LOCK) {
            this.adMask.off(cc.Node.EventType.TOUCH_START, this._onTouchStar0, this);
            // console.log('this._index0', this._listData.name, index);
            this.adBg.active = true;
            this.adBox.getComponent(cc.Button).interactable = true;
            this.adMask.active = false;
            this.adUnlocked.active = false;
            this.adReceived.active = true;
        } else if (this._curStatus.adBox == OFFICERITEM.RECEICE || index == 'adBox') {//已领取 观看完广告后进入
            this.adMask.off(cc.Node.EventType.TOUCH_START, this._onTouchStar0, this);
            // console.log('this._index1', this._listData.name, index);
            this.adBg.active = false;
            this.adBox.getComponent(cc.Button).interactable = false;
            this.adMask.active = true;
            this.adUnlocked.active = false;
            this.adReceived.active = false;
            this.Received.active = true;
        } else if (this._curStatus.adBox == OFFICERITEM.NOT) {
            this.adMask.on(cc.Node.EventType.TOUCH_START, this._onTouchStar0, this);
            this.adBg.active = false;
            this.adMask.active = true;
            this.adUnlocked.active = true;
            this.adReceived.active = false;
            this.Received.active = false;
        }
    }

    protected _onTouchStar0(event: cc.Event.EventTouch) {
        GameMgr.jumpToMgr.jumpGoTo(VIEW_ID.previewCtrl, this._listData, BOXPREVIEW.OFFICEBOX);
    }

    /**设置宝箱图片 */
    protected initBoxImg(index?: string) {
        if (this._curStatus.box == OFFICERITEM.LOCK) {
            ShaderUtil.normal(this.offLv);
            this.boxMask.off(cc.Node.EventType.TOUCH_START, this._onTouchStar0, this);
            // console.log('this._index2', this._listData.name, index);
            this.boxBg.active = true;
            this.boxMask.active = false;
            this.boxUnlocked.active = false;
            this.box.getComponent(cc.Button).interactable = true;
        } else if (this._curStatus.box == OFFICERITEM.RECEICE || index == 'box') {//已领取 点击宝箱后进入
            this.boxMask.off(cc.Node.EventType.TOUCH_START, this._onTouchStar0, this);
            // console.log('this._index3', this._listData.name, index);
            this.boReceived.active = true;
            this.boxMask.active = true;
            this.boxUnlocked.active = false;
            this.box.getComponent(cc.Button).interactable = false;
            this.boxBg.active = false;
        } else if (this._curStatus.box == OFFICERITEM.NOT) {
            ShaderUtil.gray(this.offLv, true);
            this.boxMask.on(cc.Node.EventType.TOUCH_START, this._onTouchStar0, this);
            this.boxBg.active = false;
            this.boxMask.active = true;
            this.boxUnlocked.active = true;
            this.boxReceived.active = false;
            this.boReceived.active = false;
        }
    }

    /**设置进度条 */
    protected setLoadProgress(val: number) {
        if (!this.progressBar) return;
        this.progressBar.progress = Math.min(val, 1);
    }
}   